Form
A container that collects and submits user input through various interactive elements.
#Basic Usage
A form is a group of related input controls that allows users to provide data or configure options. When using the Form
component, be sure to wrap your input controls with the Form Element Wrapper
component to get consistent styling and access to features like label, help and error messages, invalid and disabled states, etc.
These elements typically include:
- Labels: Clearly identify each input field's purpose with concise, descriptive labels. Position labels left aligned and above the input field by default for better readability and accessibility.
- Data Inputs: Include various structured input types like Input Field , Checkbox , Radios , Select, Period Picker, etc. Check the individual component guidelines for specific usage recommendations.
- Help Text: Provide contextual guidance (e.g., tooltips, placeholders, helper text) to assist users in providing the correct information. Place help text near the relevant input field for easy reference.
- Action Bar: Enable users to take action, such as submitting the form or resetting input values. Include primary and secondary action buttons as needed.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onCancel = () => {
setValue("");
setValue1("");
};
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<>
<Form heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Save",
type: "submit",
}}
cancel={{ children: "Cancel", type: "reset", onClick: onCancel }}
noPadding
noBackground
/>
</Form>
</>
);
#Submit Form Example
Enhance the basic form with designated areas for submit. If possible, always include a "Cancel" button for users to exit.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
#Submit Form - Slim Example
A more compact version of the Submit Form, ideal for limited space (e.g Modal) or when the form has fewer fields.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form variant="slim" heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
#Submit Form - Horizontal Example
Optimizes readability by aligning labels horizontally alongside the corresponding input fields. This layout is particularly useful for forms with shorter labels or when space is constrained.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form variant="horizontal" heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
Property | Description | Defined | Value |
---|---|---|---|
idOptional | string Id applied to the form | ||
childrenOptional | element Elements to be displayed inside the form | ||
headingOptional | string The heading for the form | ||
invalidOptional | boolean Is the form in an invalid state | ||
errorsOptional | string[] Error descriptions for describing the invalid state of the form | ||
hideErrorListOptional | boolean Should the error descriptions be hidden | ||
variantOptional | "horizontal" | "regular" | "slim" Form styling options | ||
onSubmitOptional | function Callback for onSubmit event | ||
data-componentOptional | string Name of the component. Should only be set by components since it needs to stable. Used to track component usage | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) | ||
data-observe-keyOptional | string Unique string, used by external script e.g. for event tracking |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications